package com.qianfeng.openapi.web.master.controller;

import com.github.pagehelper.PageInfo;
import com.qianfeng.openapi.web.master.bean.AjaxResponse;
import com.qianfeng.openapi.web.master.bean.TableData;
import com.qianfeng.openapi.web.master.pojo.UserToken;
import com.qianfeng.openapi.web.master.service.UserTokenService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @Author LJF
 * @Date 2020/7/3 11:41
 * @Version 1.0
 */
@RestController
@RequestMapping("/sys/userToken")
@CrossOrigin(allowCredentials = "true")
public class UserTokenController {

    @Autowired
    private UserTokenService userTokenService;

    @RequestMapping("/table")
    public TableData<UserToken> table(@RequestParam(defaultValue = "1") Integer page, @RequestParam(defaultValue = "5") Integer limit) {
        PageInfo<UserToken> pageInfo = userTokenService.getUserTokenByPage(page, limit);
        TableData<UserToken> tableData = new TableData<>();
        tableData.setCount(pageInfo.getTotal());
        tableData.setData(pageInfo.getList());
        return tableData;
    }

    @RequestMapping("/add")
    public AjaxResponse add(@RequestBody UserToken userToken) {
        try {
            userTokenService.insertUserToken(userToken);
            return new AjaxResponse(true, "添加成功");
        } catch (Exception ex) {
            ex.printStackTrace();
            return new AjaxResponse(false, "添加失败");
        }
    }


    @RequestMapping("/update")
    public AjaxResponse update(@RequestBody UserToken userToken) {
        try {
            userTokenService.updateUserToken(userToken);
            return new AjaxResponse(true, "更新成功");
        } catch (Exception ex) {
            ex.printStackTrace();
            return new AjaxResponse(false, "更新失败");
        }
    }

    @RequestMapping("/delete")
    public AjaxResponse delete(Integer[] ids) {
        try {
            userTokenService.deleteUserTokens(ids);
            return new AjaxResponse(true, "删除成功");
        } catch (Exception ex) {
            ex.printStackTrace();
            return new AjaxResponse(false, "删除失败");
        }
    }

    @RequestMapping("/search")
    public TableData<UserToken> search(@RequestParam(defaultValue = "1") Integer page,
                                            @RequestParam(defaultValue = "5") Integer limit,
                                            Integer appId, Integer userId,String accessToken) {
        PageInfo<UserToken> pageInfo = userTokenService.selectUserTokenByCondition(page, limit, appId, userId,accessToken);
        TableData<UserToken> tableData = new TableData<>();
        tableData.setCount(pageInfo.getTotal());
        tableData.setData(pageInfo.getList());
        return tableData;

    }

}
